package mydsl;

public class BTNode<E> implements Position<E>{
	// 该节点元素
	private E element;
	// 关系表示，左右节点与父节点
	private BTNode<E> left, right, parent;
	
	// 默认构造器
	public BTNode() {}
	
	public BTNode(E e) {
		this(e, null, null, null);
	}
	
	// 构造器
	public BTNode(E e, BTNode<E> parent, 
			BTNode<E> left, BTNode<E> right) {
		this.element = e;
		this.parent = parent;
		this.left = left;
		this.right = right;
	}
	
	public void expand() throws IllegalAccessException {
		if(this.left != null || this.right != null)
			throw new IllegalAccessException("You can't expand an internal node");
		
		this.left = new BTNode<E>(null, this, null, null);
		this.right = new BTNode<E>(null, this, null, null); 
	}
	
	/** 获得该位置的元素 */
	public E element() {
		return this.element;
	}
	/** 设置该位置元素 */
	public void setElement(E e) {
		this.element = e;
	}
	/** 获得左节点 */
	public BTNode<E> getLeft() {
		return this.left;
	}
	/** 设置左节点 */
	public void setLeft(BTNode<E> v) {
		this.left = v;
	}
	/** 获得右节点 */
	public BTNode<E> getRight() {
		return this.right;
	}
	/** 设置右节点 */
	public void setRight(BTNode<E> v) {
		this.right = v;
	}
	/** 获得父节点 */
	public BTNode<E> getParent() { 
		return this.parent; 
	}
	/** 设置父节点 */
	public void setParent(BTNode<E> v) { 
		this.parent = v; 
	}
	
	public boolean hasLeft() {
		return this.left != null;
	}
	public boolean hasRight() {
		return this.right != null;
	}
	
	public String toString() {
		return this.element.toString();
	}
}
